<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    @register="register"
    @ok="handSave"
    :okAuth="'m8:order:pic:setSize'"
    width="40%"
  >
    <template #title>
      <!-- <Icon :icon="getTitle.icon" class="pr-1 m-1" /> -->
      <span> 下料尺寸登记 【{{ getTitle.title }}】 </span>
    </template>
    <BasicForm @register="registerForm" />
    <BasicModal
      @register="register2"
      v-bind="$attrs"
      @ok="handleOk"
      :canFullscreen="false"
      okText="是"
      width="40%"
      :showCancelBtn="false"
    >
      <!-- @cancel="closeFunc"  cancelText="否"-->
      <div
        style="text-align: center; height: 200px; line-height: 200px; font-size: 60px; color: red"
      >
        是否全部完成
      </div>
      <template #centerFooter>
        <a-button :loading="loading" @click="closeFunc()">
          <Icon icon="ant-design:close-outlined" /> 否
        </a-button>
      </template>
    </BasicModal>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'WorkCalendarModal',
  });
</script>
<script setup lang="ts">
  import { defineComponent, ref, computed } from 'vue';
  import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { officeTreeData } from '/@/api/sys/office';
  // import { M8OrderPicFlowOver, overForm } from '/@/api/m8/flow/flow';
  // import { M8OrderPic, orderPicForm } from '/@/api/m8/flow/flow';
  
  import { Icon } from '/@/components/Icon';

  import { orderPicSetXlSize,M8OrderPic, orderPicForm} from '/@/api/m8/order/order';
  const emit = defineEmits(['success', 'register']);
  const { showMessage } = useMessage();
  const record = ref<M8OrderPic>({} as M8OrderPic);
  const getTitle = computed(() => ({
    title: '',
  }));
  let loading = ref(false);
  const [register2, { openModal, setModalProps: setModalProps2, closeModal: closeModal2 }] =
    useModal();

  const [register, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await orderPicForm(data);
    record.value = (res.m8OrderPic || {}) as M8OrderPic;
    record.value.xlSizeUser = record.value.xlSizeUser || data.xlSizeUser
    record.value.__t = new Date().getTime();
    // if (record.value.oper && record.value.oper.brk == '1') {
    //   updateSchema({
    //     field: 'useTime',
    //     rules: [{ required: false }, { pattern: /^\d+$/, message: '请输入一个正整数' }],
    //   });
    // }
    // if (record.value.status == 'bd') {
    //   updateSchema({
    //     field: 'createDate',
    //     required: true,
    //     show: true,
    //   });
    // } else {
    //   updateSchema({
    //     field: 'createDate',
    //     required: false,
    //     show: false,
    //   });
    // }
    // if (record.value.oper.operCode === 'KZ') {
    //   updateSchema({
    //     field: 'remarks',
    //     required: true,
    //     componentProps: {
    //       placeholder: '请填写刻字号段',
    //     },
    //   });
    // } else if (record.value.oper.operCode === 'XL' || record.value.oper.operCode === 'JGXL') {
    //   updateSchema({
    //     field: 'remarks',
    //     required: true,
    //     componentProps: {
    //       placeholder: '请填写【材料批号】,不存在时填【-】',
    //     },
    //   });
    // } else {
    //   updateSchema({
    //     field: 'remarks',
    //     required: false,
    //     componentProps: {
    //       placeholder: '请填写完工备注说明',
    //     },
    //   });
    // }
    getTitle.value.title =
      record.value.picno +
      '|' +
      record.value.picname ;
      console.log(record.value,'6666');
      
    setFieldsValue(record.value);

    document.getElementById('form_item_xlSize')?.focus();
    setDrawerProps({ loading: false });
  });
  async function handleSubmit() {
    await validate();
    openModal(true, '');
  }
  async function handleOk() {
    handSave('1');
  }
  async function closeFunc() {
    handSave('0');
  }

  async function handSave(type) {
    try {
      const data = await validate();
      setModalProps2({ confirmLoading: true });
      loading.value = true;
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // data.boverOper = type;
      // data.status = record.value.status;
      const res = await orderPicSetXlSize(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', '');
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage('您填写的信息有误，请根据提示修正。');
      }
      console.log('error', error);
    } finally {
      loading.value = false;
      setModalProps2({ confirmLoading: false });
      setDrawerProps({ confirmLoading: false });
      setTimeout(closeModal2);
    }
  }

  const inputFormSchemas: FormSchema[] = [
    {
      label: '下料尺寸',
      field: 'xlSize',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
    },
    {
      label: '下料数量',
      field: 'xlSizeQty',
      component: 'InputNumber',
      componentProps: {
        maxlength: 100,
      },
      rules: [{ required: true }, { pattern: /^\d+$/, message: '请输入一个正整数'}],
    },
    {
      label: '负责人',
      field: 'xlSizeUser',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
    },
    // {
    //   label: '图纸',
    //   field: 'orderPic.id',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 100,
    //   },
    //   required: true,
    //   show: false,
    // },
    // {
    //   label: '流程',
    //   field: 'orderFlow.id',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 100,
    //   },
    //   required: true,
    //   show: false,
    // },
    // {
    //   label: '订单',
    //   field: 'order.id',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 100,
    //   },
    //   required: true,
    //   show: false,
    // },
 
    // {
    //   label: '负责人',
    //   field: 'manCodes',
    //   fieldLabel: 'manNames',
    //   component: 'TreeSelect',
    //   required: true,
    //   componentProps: {
    //     api: officeTreeData,
    //     params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
    //     canSelectParent: false,
    //     allowClear: true,
    //   },
    // },
    // {
    //   label: '是否尺寸标注',
    //   field: 'xlSizeFlag',
    //   component: 'Select',
    //   componentProps: {
    //     dictType: 'sys_yes_no',
    //   },
    //   required: true,
    // },
    {
      label: '下料说明',
      field: 'xlSizeRemarks',
      component: 'InputTextArea',
      //required: false,
      componentProps: {
        maxlength: 500,
        rows: 5,
      },
      // colProps: { lg: 24, md: 24 },
    },
   
  ];
  const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });
</script>
<style scoped>
  .ant-picker {
    width: 80%;
  }
</style>
